{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 2 6 1]\n",
      " [3 9 6 1]\n",
      " [0 1 9 0]]\n",
      "[[0 9 3 4]\n",
      " [0 0 4 1]\n",
      " [7 3 2 4]]\n",
      "--------------------------------------------------\n",
      "[[2 2 6 1]\n",
      " [3 9 6 1]\n",
      " [0 1 9 0]\n",
      " [0 9 3 4]\n",
      " [0 0 4 1]\n",
      " [7 3 2 4]]\n",
      "[[2 2 6 1 0 9 3 4]\n",
      " [3 9 6 1 0 0 4 1]\n",
      " [0 1 9 0 7 3 2 4]]\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "# np里边的合并\n",
    "np.random.seed(123)\n",
    "arr1 = np.random.randint(0, 10, (3, 4))\n",
    "arr2 = np.random.randint(0, 10, (3, 4))\n",
    "\n",
    "print(arr1)\n",
    "print(arr2)\n",
    "print('-' * 50)\n",
    "\n",
    "print(np.concatenate([arr1, arr2]))\n",
    "print(np.concatenate([arr1, arr2], axis=1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-03T04:21:01.169469700Z",
     "start_time": "2024-05-03T04:21:01.132529Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    2\n",
      "1    2\n",
      "2    6\n",
      "3    1\n",
      "4    3\n",
      "dtype: int32\n",
      "--------------------------------------------------\n",
      "5    9\n",
      "6    6\n",
      "7    1\n",
      "8    0\n",
      "dtype: int32\n",
      "--------------------------------------------------\n",
      "9     1\n",
      "10    9\n",
      "11    0\n",
      "dtype: int32\n"
     ]
    }
   ],
   "source": [
    "# index 没有重复的情况\n",
    "np.random.seed(123)\n",
    "ser_obj1 = pd.Series(np.random.randint(0, 10, 5), index=range(0,5))\n",
    "ser_obj2 = pd.Series(np.random.randint(0, 10, 4), index=range(5,9))\n",
    "ser_obj3 = pd.Series(np.random.randint(0, 10, 3), index=range(9,12))\n",
    "\n",
    "print(ser_obj1)\n",
    "print('-' * 50)\n",
    "\n",
    "print(ser_obj2)\n",
    "print('-' * 50)\n",
    "\n",
    "print(ser_obj3)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-03T04:29:57.438343400Z",
     "start_time": "2024-05-03T04:29:57.421013Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0     2\n",
      "1     2\n",
      "2     6\n",
      "3     1\n",
      "4     3\n",
      "5     9\n",
      "6     6\n",
      "7     1\n",
      "8     0\n",
      "9     1\n",
      "10    9\n",
      "11    0\n",
      "dtype: int32\n"
     ]
    }
   ],
   "source": [
    "print(pd.concat([ser_obj1, ser_obj2, ser_obj3]))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-03T04:30:14.853691Z",
     "start_time": "2024-05-03T04:30:14.835711700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      0    1    2\n",
      "0   2.0  NaN  NaN\n",
      "1   2.0  NaN  NaN\n",
      "2   6.0  NaN  NaN\n",
      "3   1.0  NaN  NaN\n",
      "4   3.0  NaN  NaN\n",
      "5   NaN  9.0  NaN\n",
      "6   NaN  6.0  NaN\n",
      "7   NaN  1.0  NaN\n",
      "8   NaN  0.0  NaN\n",
      "9   NaN  NaN  1.0\n",
      "10  NaN  NaN  9.0\n",
      "11  NaN  NaN  0.0\n"
     ]
    }
   ],
   "source": [
    "print(pd.concat([ser_obj1, ser_obj2, ser_obj3], axis=1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-03T04:30:25.277399900Z",
     "start_time": "2024-05-03T04:30:25.248240400Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### index 有重复的情况"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    2\n",
      "1    2\n",
      "2    6\n",
      "3    1\n",
      "4    3\n",
      "dtype: int32\n",
      "0    9\n",
      "1    6\n",
      "2    1\n",
      "3    0\n",
      "dtype: int32\n",
      "0    1\n",
      "1    9\n",
      "2    0\n",
      "dtype: int32\n"
     ]
    }
   ],
   "source": [
    "# index 有重复的情况\n",
    "np.random.seed(123)\n",
    "ser_obj1 = pd.Series(np.random.randint(0, 10, 5), index=range(5))\n",
    "ser_obj2 = pd.Series(np.random.randint(0, 10, 4), index=range(4))\n",
    "ser_obj3 = pd.Series(np.random.randint(0, 10, 3), index=range(3))\n",
    "\n",
    "print(ser_obj1)\n",
    "print(ser_obj2)\n",
    "print(ser_obj3)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-03T04:32:10.987947Z",
     "start_time": "2024-05-03T04:32:10.941170900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    2\n",
      "1    2\n",
      "2    6\n",
      "3    1\n",
      "4    3\n",
      "0    9\n",
      "1    6\n",
      "2    1\n",
      "3    0\n",
      "0    1\n",
      "1    9\n",
      "2    0\n",
      "dtype: int32\n"
     ]
    }
   ],
   "source": [
    "# 相同索引名直接往下排\n",
    "print(pd.concat([ser_obj1, ser_obj2, ser_obj3]))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-03T04:33:20.230876600Z",
     "start_time": "2024-05-03T04:33:20.207531500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   0  1  2\n",
      "0  2  9  1\n",
      "1  2  6  9\n",
      "2  6  1  0\n"
     ]
    }
   ],
   "source": [
    "# inner 和outer 代表内连接和全外连接\n",
    "print(pd.concat([ser_obj1, ser_obj2, ser_obj3], axis=1, join='inner'))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-03T04:34:14.073049700Z",
     "start_time": "2024-05-03T04:34:14.042303500Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### df的concat"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A  B\n",
      "a  2  2\n",
      "b  6  1\n",
      "c  3  9\n",
      "   A  B\n",
      "a  6  1\n",
      "b  0  1\n",
      "--------------------\n",
      "   A  B\n",
      "a  2  2\n",
      "b  6  1\n",
      "c  3  9\n",
      "a  6  1\n",
      "b  0  1\n",
      "   A  B    A    B\n",
      "a  2  2  6.0  1.0\n",
      "b  6  1  0.0  1.0\n",
      "c  3  9  NaN  NaN\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "np.random.seed(123)\n",
    "df_obj1 = pd.DataFrame(np.random.randint(0, 10, (3, 2)), index=['a', 'b', 'c'],\n",
    "                       columns=['A', 'B'])\n",
    "df_obj2 = pd.DataFrame(np.random.randint(0, 10, (2, 2)), index=['a', 'b'],\n",
    "                       columns=['A', 'B'])\n",
    "print(df_obj1)\n",
    "print(df_obj2)\n",
    "print('-' * 20)\n",
    "\n",
    "print(pd.concat([df_obj1, df_obj2]))\n",
    "print(pd.concat([df_obj1, df_obj2], axis=1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-03T04:42:00.908922900Z",
     "start_time": "2024-05-03T04:42:00.849377400Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [
     "# 11 数据合并(pd.concat)\n"
    ],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
